<template>
  <div class="notice-container">
    <div class="search-container">
      <a-input-search class="search-input" placeholder="请输入关键词" enter-button="搜索" size="large" @search="onSearch"/>
    </div>
    <div class="list-container">
      <div class="notice-item" v-for="(notice,index) in notices" :key="index" @click="noticeDetail(notice.id)">
        <div class="notice-image">
          <img :src="notice.image" :alt="notice.title">
        </div>
        <div class="notice-title">
          <span>{{ notice.title }}</span>
        </div>
        <div class="notice-describe">
          <span>{{ notice.describe }}</span>
        </div>
        <div class="notice-operate">
          <span>{{ notice.created_at }}</span>
        </div>
      </div>
    </div>
    <div class="page-container">
      <a-pagination :current="page" :total="total" :defaultPageSize="pageSize" @change="onChange"/>
    </div>

    <!--加载框-->
    <a-spin class="loading" tip="Loading..." spinning v-if="loading"/>
  </div>
</template>

<script>
export default {
  name: 'Notice',
  data () {
    return {
      loading: true,
      notices: [],
      searchKeyword: '',
      page: 1,
      pageSize: 8,
      total: 0
    }
  },
  created () {
    this.noticeLists()
  },
  methods: {
    noticeLists () {
      this.loading = true
      this.$http.get('/news', {
        params: {
          page: this.page,
          pageSize: this.pageSize,
          searchKeyword: this.searchKeyword
        }
      }).then(res => {
        this.page = res.data.data.page
        this.pageSize = res.data.data.pageSize
        this.total = res.data.data.total
        this.notices = res.data.data.lists
        this.loading = false
      })
    },
    noticeDetail (noticeId) {
      this.$router.push({ path: '/NoticeDetail', query: { noticeId: noticeId } })
    },
    onSearch (keyword) {
      this.searchKeyword = keyword
      this.noticeLists()
    },
    onChange (page) {
      this.page = page
      this.noticeLists()
    }
  }
}
</script>

<style scoped lang="less">
@searchContainerHeight: 100px;
@pageContainerHeight: 100px;
@noticeItemWidthAndHeight: 250px;
.notice-container {
  width: 100%;
  height: 100%;
  position: relative;
}

.search-container {
  width: 100%;
  height: @searchContainerHeight;
  display: flex;
  justify-content: center;
  align-items: flex-end;
}

.search-input {
  width: 500px;
  padding-bottom: 10px;
}

.list-container {
  width: 100%;
  height: calc(100% - @searchContainerHeight - @pageContainerHeight);
  overflow: auto;
  display: flex;
  flex-flow: row wrap;
  justify-content: space-evenly;
  align-items: center;
  grid-gap: 20px 20px;
  padding: 20px;
}

@media all and (min-width: 1400px) {
  .list-container {
    display: grid;
    grid-template-columns: repeat(4, @noticeItemWidthAndHeight);
    grid-template-rows: repeat(2, @noticeItemWidthAndHeight);
    align-content: space-around;
  }
}

.notice-item {
  width: @noticeItemWidthAndHeight;
  height: @noticeItemWidthAndHeight;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: black 0 0 20px -10px;
  cursor: pointer;
  transition: 0.3s;
  flex-shrink: 0;
}

.notice-item:hover {
  box-shadow: black 0 0 20px -5px;
}

.notice-image {
  width: 100%;

  img {
    width: 100%;
    height: 125px;
  }
}

.notice-title {
  width: 100%;
  padding: 10px 10px 5px 10px;
  font-weight: bold;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}

.notice-describe {
  width: 100%;
  padding: 0 10px;
  margin-bottom: 10px;
  height: 45px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.notice-operate {
  width: 100%;
  padding: 0 10px 10px 10px;
}

.page-container {
  width: 100%;
  height: @pageContainerHeight;
  display: flex;
  justify-content: center;
  align-items: center;
}

.loading {
  position: absolute;
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
  display: flex;
  background-color: rgba(255, 255, 255, 0.5);
  justify-content: center;
  align-items: center;
  flex-flow: column;
}
</style>
